<template>
  <div class="box">
    <div class="topboxone">
      <el-button type="primary" @click="addbutton">新增随访</el-button>
    </div>
    <div>
      <el-table :data="datalist" border style="width: 100%" max-height="700" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="expand">
          <template #default="props">
            <div m="4">
              <p m="t-0 b-2">患者主诉: {{ props.row.patientComplaint }}</p>
              <p m="t-0 b-2">解决方案: {{ props.row.solution }}</p>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="followUpPurposeName" label="随访目的" />
        <el-table-column prop="followUpDate" label="随访日期" />
        <el-table-column prop="followUpMethodName" label="随访方式" />
        <el-table-column prop="followUpStatusName" label="随访状态" />
        <el-table-column prop="followUpBy" label="随访人" />

        <el-table-column prop="followUpBy" label="操作">
          <template #default="scope">
            <el-button type="primary" @click="deletebutton(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="fybox">
        <el-pagination
          v-model:current-page="formInline.pageNum"
          v-model:page-size="formInline.pageSize"
          :page-sizes="[10, 50, 100, 200]"
          layout="total, sizes, prev, pager"
          :total="pagetotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="新增随访" width="700" :before-close="handleClose">
      <el-form :model="form" label-width="auto" style="max-width: 600px" ref="ruleFormRef">
        <el-form-item label="随访方式" prop="followUpMethod">
          <el-select v-model="form.followUpMethod">
            <el-option v-for="item in ssfslist" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="随访目的" prop="followUpPurpose">
          <el-radio-group v-model="form.followUpPurpose">
            <el-radio v-for="option in sfmdlist" :key="option.dictValue" :value="option.dictValue" size="large">
              {{ option.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="随访状态" prop="followUpStatus">
          <el-select v-model="form.followUpStatus">
            <el-option v-for="item in sfstatus" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="随访日期">
          <el-date-picker v-model="form.followUpDate" value-format="YYYY-MM-DD" type="date" style="width: 240px" />
        </el-form-item>

        <el-form-item label="患者主诉">
          <el-input v-model="form.patientComplaint"></el-input>
        </el-form-item>
        <el-form-item label="解决方案">
          <el-input v-model="form.solution"></el-input>
        </el-form-item>
        <el-form-item label="预约日期">
          <el-date-picker v-model="form.reservationDate" value-format="YYYY-MM-DD" type="date" style="width: 240px" />
        </el-form-item>
        <el-form-item label="随访人">
          <el-input v-model="form.followUpBy"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>
        <el-form-item>
          <div class="savrnutton">
            <el-button type="primary" @click="savebutton(ruleFormRef)">保存</el-button>
            <el-button @click="clearbutton(ruleFormRef)">清空</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { addsfAPI, updatesfAPI, getsflistApi, deltesfApi } from "@/api/hzmange/index.js";
const route = useRoute();
const routedata = reactive(route.query);
const YZlist = JSON.parse(sessionStorage.getItem("YZlist"));
let ssfslist = ref([]); //随访方式
let sfmdlist = ref([]); //随访目的
let sfstatus = ref([]); //随访状态
sfmdlist.value = YZlist.follow_up_purpose;
ssfslist.value = YZlist.follow_up_method;
sfstatus.value = YZlist.follow_up_status;
let datalist = ref([]);
let dialogVisible = ref(false);
const ruleFormRef = ref(null);
const size = ref("default");
var pagetotal = ref(0);
let handleSizeChange = () => {
  getlist();
};
let handleCurrentChange = () => {
  getlist();
};
//获取字典列表
let formInline = ref({
  pageNum: 1,
  pageSize: 10,
  patientName: routedata.name
});
const form = ref({
  patientName: "", //患者姓名
  patientId: "", //患者ID
  followUpMethod: "", //随访方式
  followUpPurpose: "", //随访目的
  followUpStatus: "", //随访状态
  followUpDate: "", //随访日期
  patientComplaint: "", //患者主诉
  solution: "", //解决方案
  reservationDate: "2025-05-23", //预约日期
  followUpBy: "", //随访人
  remark: ""
});
const onSubmit = formEl => {
  dialogVisible.value = false;
};
const clearbutton = formEl => {
  if (!formEl) return;
  formEl.resetFields();
};
getCurrentDate();
function getCurrentDate() {
  const now = new Date();

  const year = now.getFullYear();
  // 因为getMonth()返回的月份是从0开始计数的，所以需要加1
  const month = now.getMonth() + 1;
  const day = now.getDate();

  // 将日期格式化为 "YYYY-M-D"
  form.value.Datevalue = `${year}-${month}-${day}`;
}
const handleClose = () => {
  dialogVisible.value = false;
};
let addbutton = () => {
  form.value = {
    patientName: "", //患者姓名
    patientId: "", //患者ID
    followUpMethod: "", //随访方式
    followUpPurpose: "", //随访目的
    followUpStatus: "", //随访状态
    followUpDate: "", //随访日期
    patientComplaint: "", //患者主诉
    solution: "", //解决方案
    reservationDate: "2025-05-23", //预约日期
    followUpBy: "", //随访人
    remark: ""
  };
  dialogVisible.value = true;
};
const seebutton = e => {
  dialogVisible.value = true;
};
let xgstatus = ref(false);
function savebutton(formEl) {
  if (!formEl) return;
  formEl.validate(valid => {
    if (valid) {
      form.value.patientName = routedata.name;
      form.value.patientId = routedata.id;
      //修改
      if (xgstatus.value == true) {
        updatesfAPI(form.value).then(() => {
          ElMessage({
            message: "修改成功",
            type: "success"
          });
          formEl.resetFields();
          dialogVisible.value = false;
          xgstatus.value = false;
          //刷新列表数据
          getlist();
        });
      } else {
        //新增
        addsfAPI(form.value).then(() => {
          ElMessage({
            message: "新增成功",
            type: "success"
          });
          formEl.resetFields();
          dialogVisible.value = false;
          getlist();
        });
      }
    } else {
      return false;
    }
  });
}
getlist();
function getlist() {
  getsflistApi(formInline.value).then(res => {
    pagetotal.value = res.total;
    datalist.value = res.rows;
  });
}
let selection = ref([]);
const handleSelectionChange = e => {
  selection.value = e;
};
const deletebutton = e => {
  ElMessageBox.confirm("确定删除?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deltesfApi({
      id: e
    }).then(res => {
      ElMessage({
        type: "success",
        message: `${res.msg}`
      });
      getlist();
    });
  });
};
</script>
<style scoped>
.box {
  color: #606266;
}
.topbox {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.savrnutton {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}
.topboxone {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
.fybox {
  display: flex;
  justify-content: end;
}
</style>
